import { Controller, Get, Post, Body, Patch, Param, Delete, Query,Headers } from '@nestjs/common';
import { ChatService } from './chat.service';
import { CreateChatDto } from './dto/create-chat.dto';
import { UpdateChatDto } from './dto/update-chat.dto';
import { ApiHeader, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { UserInfoDto } from '../model/dto/user-info.dto';

@Controller('chat')
export class ChatController {
  constructor(private readonly chatService: ChatService) {}

  @Post()
  @ApiOperation({
    summary: 'Create a new chat',
    description: 'Create a new chat with the given message',
  })
  @ApiHeader({
    name: 'Api-Key',
    description: 'The API key to use',
    required: false,
  })
  @ApiResponse({
    status: 201,
    description: 'The chat has been successfully created',
  })
  create(
        @Body() createChatDto: CreateChatDto,
        @Headers('user') user:UserInfoDto
    ) {
    return this.chatService.create(createChatDto,user && JSON.parse(user as unknown as string)
  );
  }

  @Get()
  findAll() {
    return this.chatService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.chatService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateChatDto: UpdateChatDto) {
    return this.chatService.update(+id, updateChatDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.chatService.remove(+id);
  }
}
